package com.ace.user.module.service.impl;

import com.ace.user.exception.BusinessException;
import com.ace.user.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ace.user.common.constant.C;
import com.ace.user.config.locale.RLocale;
import com.ace.user.module.entity.SftpConfig;
import com.ace.user.module.entity.SystemConfig;
import com.ace.user.module.mapper.SystemConfigMapper;
import com.ace.user.module.service.ISystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 用户定义表 服务实现类
 * </p>
 *
 * @author jobObj
 * @since 2019-10-11
 */
@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
@Slf4j
public class SystemConfigServiceImpl extends ServiceImpl<SystemConfigMapper, SystemConfig> implements ISystemConfigService {

    @Resource
    private SftpConfig sftpConfig;

    @Resource
    private RLocale rLocale;

    @Override
    public SystemConfig getConfigByAppId(String appId) {
        SystemConfig config = this.getOne(new QueryWrapper<SystemConfig>().eq(SystemConfig.DB_APP_ID,appId));
        if (config == null) {
            config = this.getOne(new QueryWrapper<SystemConfig>().eq(SystemConfig.DB_APP_ID,"develop"));
        }
        config.setAppId(appId);
        return config;
    }


    @Override
    public Map<String, Object> sftpConfig(String uploadKey) {
        Map<String,Object> ret = new HashMap<>();
        Map<String,Object> downPath = new HashMap<>();
        String path = "";
        switch (uploadKey) {
            case "im" :
                path = String.format("%s/%s",sftpConfig.getRemotePath(),sftpConfig.getImPath());
                downPath = sftpConfig.getDownPath().get("imDown");
                break;
            case "apk":
                path = String.format("%s/%s",sftpConfig.getRemotePath(),sftpConfig.getApkPath());
                downPath = sftpConfig.getDownPath().get("apkDown");
                break;
            case "record":
                downPath = sftpConfig.getDownPath().get("recordDown");
                path = String.format("%s/%s",sftpConfig.getRemotePath(),sftpConfig.getRecordPath());
                break;
            case "att":
                downPath = sftpConfig.getDownPath().get("recordAttDown");
                path = String.format("%s/%s",sftpConfig.getRemotePath(),sftpConfig.getRecordAttPath());
                break;
            case "remote":
                path = String.format("%s/%s",sftpConfig.getRemotePath(),sftpConfig.getRemotePath());
                downPath = sftpConfig.getDownPath().get("remoteDown");
                break;
        }
        if (!StringUtil.isNotEmpty(path)) {
            throw new BusinessException(rLocale.getDTO("FILE_UPLOAD_INFO_ERR", C.F_3), rLocale.getMessage("FILE_UPLOAD_INFO_ERR"));
        }
        ret.put("port",sftpConfig.getPort());
        ret.put("host",sftpConfig.getHost());
        ret.put("password",sftpConfig.getPassword());
        ret.put("username",sftpConfig.getUsername());
        ret.put("uploadPath",path);
        ret.put("download",downPath);
        return  ret;
    }
}
